#include "udpserver.hpp"
#include <memory>
#include <functional>
#include <vector>

void Usage(const std::string &proc)
{
    std::cout << "\n\rUsage: " << proc << " serverport[1024+]\n"
              << std::endl;
}

std::string Handle(const std::string &msg)
{
    std::string echo_string = "server echo#";
    echo_string += msg;

    return echo_string;
}

std::string HandleCom(const std::string &com)
{
   std::vector<std::string> badCom={
        "rm",
        "kill",
        "touch",
        "top",
        "sleep",
        "yum",
        "sudo",
        "unlink",
        "uninstall"
    };

    for(auto &s:badCom){
        auto it = com.find(s);
        if(it != string::npos){
            return "Bad Man";
        }
    }

    FILE* fp = popen(com.c_str(),"r");
    if(fp == nullptr){
        lg(Fatal,"popen failed");
        return "Error";
    }

    std::string ret;
    while(true){
        char buf[4096] = {0};
        char *ok = fgets(buf,sizeof(buf),fp);   //遇到换行符会提前结束
        if(ok == nullptr){
            break;
        } 

        ret +=buf;
    }
    
    return ret;
}


int main(int argc,char *argv[])
{
    if(argc != 2){
        Usage(argv[0]);
        exit(0);
    }

    uint16_t serverport = std::stoi(argv[1]);

    std::unique_ptr<updserver> utp(new updserver(serverport));
    utp->Init();
    
    // utp->Start(std::bind(Handle,std::placeholders::_1));
    utp->Start();

    return 0;
}